<?php 
	class NotificationModel
	{

		function __construct()
		{

		}
		function user_database_changed()
		{
			
		}
		function delete_notification($actor_id,$receiver_id,$notification_type,$item_id)
		{
			global $wpdb;
			$wpdb->delete( $wpdb->prefix.'notifications', array( 'actor_id' => $actor_id,'receiver_id'=>$receiver_id,'type' => $notification_type,'object_id'=>$item_id ) );
		}
		function delete_comment_notification($actor_id,$receiver_id,$notification_type,$item_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT distinct * FROM ".$wpdb->prefix.'bwg_image_comment WHERE image_id=%d AND name=%s',$item_id,$actor_id);
			$rows=$wpdb->get_results($query);
			$wpdb->delete( $wpdb->prefix.'notifications', array( 'actor_id' => $actor_id,'receiver_id'=>$receiver_id,'type' => $notification_type,'object_id'=>$item_id ) );
		}
		function insert_notification($actor_id,$receiver_id,$notification_type,$item_id)
		{

			global $wpdb;
			$created_date = date("Y-m-d H:i:s");
			$query=$wpdb->prepare("SELECT   *  FROM ".$wpdb->prefix.'notifications WHERE actor_id=%d AND receiver_id=%d AND object_id=%d AND type=%s AND status=0',$actor_id,$receiver_id,$item_id,$notification_type);
			$rows=$wpdb->get_results($query);
			if(count($rows)>0) return;
			$wpdb->insert( 
					$wpdb->prefix.'notifications',
					array( 'id' => 'DEFAULT','actor_id'=>$actor_id,'receiver_id' => $receiver_id,'type'=>$notification_type,'object_id'=>$item_id,'status'=>0,'date'=>$created_date), 
					array( '%d' ,'%d','%d','%s','%d','%d','%s')
				);
		}
		//nhung nguoi da thich hoac binh luan chua xem
		function get_counter($user_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT distinct object_id as counter FROM ".$wpdb->prefix.'notifications WHERE receiver_id=%d AND type=%s AND status=0',$user_id,$type);
			$rows=$wpdb->get_results($query);
			return count($rows);
		}
		//tra ve so luong tat ca nhung nguoi da thich hoac binh luan tren anh
		function get_all_counter($user_id,$image_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT  count(*) as counter FROM ".$wpdb->prefix.'notifications WHERE receiver_id=%d AND object_id=%d AND type=%s ',$user_id,$image_id,$type);
			$row=$wpdb->get_row($query);
			return $row->counter;
		}
//nhung nguoi da thich hoac binh luan chua xem
		function get_new_notification_images($user_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT  distinct object_id FROM ".$wpdb->prefix.'notifications WHERE receiver_id=%d AND type=%s',$user_id,$type);
			$rows=$wpdb->get_results($query);
			return $rows;
		}
		function get_old_notification_images($user_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT  distinct object_id FROM ".$wpdb->prefix.'notifications WHERE receiver_id=%d AND type=%s AND status=1',$user_id,$type);
			$rows=$wpdb->get_results($query);
			return $rows;
		}
		function get_last_actor($user_id,$image_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT  * FROM ".$wpdb->prefix.'notifications as notify,'.$wpdb->prefix.'users as users WHERE notify.actor_id=users.ID AND receiver_id=%d AND object_id=%d AND type=%s ORDER BY date desc LIMIT 1',$user_id,$image_id,$type);
			$row=$wpdb->get_row($query);
			return $row;
		}
		function update_seen_status($receiver_id,$type)
		{
			global $wpdb;
			$wpdb->update( 
					$wpdb->prefix.'notifications', 
					array( 
						'status' => 1,
					), 
					array( 'receiver_id' => $receiver_id ,'type' => $type ), 
					array('%d'),
					array( '%d' ,'%s')
				);
		}
		function get_user_ip($user_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'user_ip WHERE user_id=%d ',$user_id);
			$row=$wpdb->get_row($query);
			return $row->ip;
		}

		function is_user_notification_exist($user_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users_notifications WHERE user_id=%d AND type=%s ',$user_id,$type);
			$row=$wpdb->get_results($query);
			if($row) return 1;
			else return 0;
		}
		function is_item_exist($notification_id, $item_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'notification_items WHERE notification_id=%d AND item_id=%d',$user_id,$item_id);
			$row=$wpdb->get_results($query);
			if($row) return 1;
			else return 0;
		}
		function insert_noification_item($notification_id, $item_id,$activate_user)
		{
			global $wpdb;
			$wpdb->insert( 
					$wpdb->prefix.'notification_items',
					array( 'id' => 'DEFAULT' ,'notification_id' => $notification_id,'item_id'=>$item_id,'activate_user'=>$activate_user), 
					array( '%d','%d' ,'%d','%d')
				);
		}
		function delete_noification_item($notification_id, $item_id)
		{
			global $wpdb;
			$wpdb->delete( $wpdb->prefix.'notification_items', array( 'notification_id' => $notification_id,'item_id'=>$item_id ) );
		}
		function get_notification($user_id,$type)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'users_notifications WHERE user_id=%d AND type=%s ',$user_id,$type);
			$row=$wpdb->get_results($query);
			return $row;
		}
		//Kiem tra xem da ton tai notification chua de them hoac xoa
		function is_notification_exist($activate_user,$author,$notification_type,$item_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'notifications WHERE actor_id=%d AND receiver_id=%d AND object_id=%d AND type=%s ',$user_id,$type);
			$row=$wpdb->get_results($query);
			if($row) return 1;
			else return 0;
		}
		
		function update_notification($user_id,$type)
		{
			global $wpdb;
			$wpdb->update( 
					$wpdb->prefix.'users_notifications', 
					array( 
						'status' => $status,
					), 
					array( 'follower_id' => $follower_id ,'author_id' => $user_id ), 
					array('%d'),
					array( '%d' ,'%d')
				);
		}
		function get_image_author_id($image_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'bwg_image WHERE id=%d',$image_id);
			$row=$wpdb->get_row($query);
			return $row->author;
		}
		function get_image($image_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT * FROM ".$wpdb->prefix.'bwg_image WHERE id=%d',$image_id);
			$row=$wpdb->get_row($query);
			return $row;
		}
		function get_image_commented_users($image_id)
		{
			global $wpdb;
			$query=$wpdb->prepare("SELECT distinct name FROM ".$wpdb->prefix.'bwg_image_comment WHERE image_id=%d',$image_id);
			$rows=$wpdb->get_results($query);
			$user_ids=array();
			foreach ($rows as $row) {
				$query=$wpdb->prepare("SELECT ID FROM ".$wpdb->prefix.'users WHERE user_login=%s',$row->name);
				$user_id_row=$wpdb->get_row($query);
				$user_ids[]=$user_id_row->ID;
			}

			return $user_ids;
		}

	}
?>